//商品列表
var app = getApp(); //全局APP

Page({
	//页面的初始数据
	data: {
		nickname: '',
		avatar: '../../image/default_avatar.png',
		listData: [],
		selectNum: 0,
		Specicalties: [],
		SpecicaltyIds: [],
		Classes: [],
		ClassIds: [],
		Dirs: [],
		DirsIds: [],
		specicaltityIndex: 0,
		classIndex: 0,
		dirIndex: 0,
		checkboxAll: false,
		filterDlg: false,
	},

	//加载执行
	onShow: function() {
		var page = this;

		// 获取用户信息
		app.api.userInfo(function(res) {
			if (res.status) {
				//如果没有头像，设置本地默认头像
				var avatar = '../../image/default_avatar.png';
				if (res.data.avatar) {
					avatar = res.data.avatar;
				}
				page.setData({
					nickname: res.data.nickname,
					avatar: avatar
				});
			}
		});

		page.getApplyClassList();
		 
		// 获取专业
		app.api.getSpecicalties(function(res) {
			if (res.status) {
				var tmp_specicalties = [];
				var tmp_specicaltyIds = [];

				for (var i = 0; i < res.data.data.length; i++) {
					tmp_specicalties[i] = res.data.data[i].zymc;
					tmp_specicaltyIds[i] = res.data.data[i].zydm;
				}

				page.setData({
					Specicalties: tmp_specicalties,
					SpecicaltyIds: tmp_specicaltyIds
				});

				// 如果专业数量大于零默认取第一个专业的班级
				page.getClassByspecicalty();
			} else {
				//报错了
				app.common.errorToBack(res.msg, 0);
				page.setData({
					Specicalties: [],
					SpecicaltyIds: []
				});
			}
		});

		// 获取方向
		app.api.getDirs(function(res) {
			if (res.status) {
				var tmp_Dirs = [];
				var tmp_DirsIds = [];

				for (var i = 0; i < res.data.data.length; i++) {
					tmp_Dirs[i] = res.data.data[i].name;
					tmp_DirsIds[i] = res.data.data[i].id;
				}

				page.setData({
					Dirs: tmp_Dirs,
					DirsIds: tmp_DirsIds
				});
			} else {
				//报错了
				app.common.errorToBack(res.msg, 0);
				page.setData({
					Dirs: [],
					DirsIds: []
				});
			}
		});
	},
	
	getApplyClassList: function() {
		var page = this;
		// 获取待提交班级列表
		app.api.getApplyClassList({}, function(res) {
			if (res.status) {
				page.setData({
					listData: res.data
				});
		
				page.isAllCheckbox();
			} else {
				//报错了
				app.common.errorToBack(res.msg, 0);
				page.setData({
					listData: []
				});
			}
		});
	},

	// 打开筛选窗口
	filter: function(e) {
		this.setData({
			filterDlg: true
		});
	},

	// 关闭筛选窗口
	close: function() {
		this.setData({
			filterDlg: false
		});
	},

	// 筛选提交
	filterSubmit: function() {
		var page = this;
		var data = {
			zydm: page.data.SpecicaltyIds[page.data.specicaltyIndex],
			bjdm: page.data.ClassIds[page.data.classIndex],
			dir: page.data.DirsIds[page.data.dirIndex]
		};

		page.getApplyClassList();
		 
		this.setData({
			filterDlg: false
		});
	},

	// 选择事件处理
	itemSelected: function(e) {
		var page = this;
		var plan_class_id = e.currentTarget.dataset.plan_class_id;

		var listData = page.data.listData;
		for (var i = 0; i < listData.length; i++) {
			if (listData[i].plan_class_id == plan_class_id) {
				if (listData[i].status == true) {
					listData[i].status = false;
				} else {
					listData[i].status = true;
				}
			}
		}

		page.setData({
			listData: listData
		});

		page.isAllCheckbox();
	},

	//是否全选
	isAllCheckbox: function() {
		var page = this;
		var listData = page.data.listData;
		var flag = true;
		var num = 0;

		if (!listData && typeof(listData) != "undefined") {
			flag = false;
			num = 0;
		} else {
			for (var i = 0; i < listData.length; i++) {
				if (listData[i].status == false) {
					flag = false;
				} else {
					num += parseInt(listData[i].num);
				}
			}

			if (listData.length <= 0) {
				flag = false;
				num = 0;
			}
		}

		this.setData({
			checkboxAll: flag,
			selectNum: num
		});
	},

	//全选操作
	checkboxAll: function(e) {
		if (this.data.checkboxAll == true) {
			this.setData({
				checkboxAll: false
			});
			this.setAllCheckbox(false);
		} else {
			this.setData({
				checkboxAll: true
			});
			this.setAllCheckbox(true);
		}
	},

	//全选设置
	setAllCheckbox: function(e) {
		var page = this;
		var listData = page.data.listData;
		if (e) {
			//全选
			for (var i = 0; i < listData.length; i++) {
				listData[i].status = true;
			}
		} else {
			//全不选
			for (var i = 0; i < listData.length; i++) {
				listData[i].status = false;
			}
		}
		page.setData({
			listData: listData
		});

		page.isAllCheckbox();
	},

	getClassByspecicalty: function() {
		var page = this;
		var data = {
			zydm: page.data.SpecicaltyIds[page.data.specicaltyIndex]
		};

		// 获取班级
		app.api.getClasses(data, function(res) {
			if (res.status) {
				var tmp_Classes = [];
				var tmp_ClassIds = [];

				for (var i = 0; i < res.data.data.length; i++) {
					tmp_Classes[i] = res.data.data[i].bjmc;
					tmp_ClassIds[i] = res.data.data[i].bjdm;
				}

				page.setData({
					Classes: tmp_Classes,
					ClassIds: tmp_ClassIds
				});
			} else {
				//报错了
				app.common.errorToBack(res.msg, 0);
				page.setData({
					Classes: [],
					ClassIds: [],
				});
			}
		});
	},

	// 绑定专业
	bindSpecicalty: function(e) {
		var page = this;
		page.setData({
			specicaltyIndex: e.detail.value
		});

		page.getClassByspecicalty();
	},

	// 绑定班级
	bindClass: function(e) {
		this.setData({
			classIndex: e.detail.value
		});
	},

	// 绑定方向
	bindDir: function(e) {
		this.setData({
			dirIndex: e.detail.value
		});
	},

	showDetail: function(e) {
		var page = this;
		var plan_class_id = e.currentTarget.dataset.plan_class_id;

		wx.navigateTo({
			url: '../BookAplyDetail/BookAplyDetail?plan_class_id=' + plan_class_id
		});
	},

	submit: function(e) {
		var page = this; 
		
		if(page.data.selectNum == 0) {
			app.common.errorToBack("没有需要提交的学生！", 0);
			return;
		}
		
		var listData = page.data.listData;
		
		var newData = '';
		for (var i = 0; i < listData.length; i++) {
			if (listData[i].status == true) {
				newData += ',' + listData[i].plan_class_id;
			} 
		}
		
		if (newData.substr(0, 1) == ',') {
			newData = newData.substr(1);
		}
		
		var data = {
			plan_class_ids: newData
		};
		
		// 提交申请
		app.api.bookApply(data, function(res) {
			if (res.status) {
				// 重新查询申请列表
				page.getApplyClassList();
			}
			
			app.common.errorToBack(res.msg, 0);
		});
	},
});
